﻿namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System.Web.UI;

    /// <summary>
    /// This is a static helper class that allows simple ViewState value handling
    /// </summary>
    public static class ViewStateHelper
    {
        /// <summary>
        /// Return a type safe value from the viewstate
        /// </summary>
        /// <param name="viewState">
        /// The view state.
        /// </param>
        /// <param name="keyName">
        /// The key name.
        /// </param>
        /// <param name="defaultValue">
        /// The default value.
        /// </param>
        /// <typeparam name="T">
        /// the type of the value
        /// </typeparam>
        /// <returns>
        /// the value from viewstate
        /// </returns>
        public static T GetValue<T>(this StateBag viewState, string keyName, T defaultValue)
        {
            if (viewState[keyName] != null)
            {
                return (T)viewState[keyName];
            }

            return defaultValue;
        }
    }
}
